<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 标题区域（样式重构） -->
      <div class="login-header">
        <div class="header-decoration"></div>
        <h1 class="login-title">后台管理系统</h1>
        <p class="login-subtitle">欢迎登录，请填写您的账号信息</p>
      </div>

      <!-- 登录表单（结构不变，样式全改） -->
      <el-form
          class="login-form"
          :model="loginForm"
          :rules="loginRules"
          ref="loginFormRef"
      >
        <!-- 账号输入 -->
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              size="large"
              class="input-custom"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              show-password
              @keyup.enter="handleLogin"
              class="input-custom"
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 记住我和忘记密码 -->
        <div class="login-options">
          <el-checkbox v-model="loginForm.remember" class="checkbox-custom">记住我</el-checkbox>
          <el-link type="primary" :underline="false" class="link-custom">忘记密码？</el-link>
        </div>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
              type="primary"
              size="large"
              class="btn-login"
              :loading="loading"
              @click="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>
              <el-icon class="loading-icon"><Loading /></el-icon>
              登录中...
            </span>
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 新增底部装饰 -->
      <div class="login-footer">
        <div class="footer-line"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock, Loading } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 所有脚本逻辑完全不变，直接保留原代码
const router = useRouter()
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})
const loginRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})
const loginFormRef = ref()
const loading = ref(false)

const handleLogin = async () => {
  if (!loginFormRef.value) return
  try {
    await loginFormRef.value.validate()
  } catch (error) {
    ElMessage.error('请填写完整的登录信息')
    return
  }
  loading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 1500))
    if (loginForm.username === 'admin' && loginForm.password === '123456') {
      localStorage.setItem('token', 'your-auth-token')
      localStorage.setItem('userInfo', JSON.stringify({
        username: loginForm.username,
        loginTime: new Date().toISOString()
      }))
      if (loginForm.remember) {
        localStorage.setItem('rememberedUser', JSON.stringify({
          username: loginForm.username
        }))
      } else {
        localStorage.removeItem('rememberedUser')
      }
      ElMessage.success('登录成功！')
      router.push('/manager')
    } else {
      ElMessage.error('账号或密码错误，请重试')
    }
  } catch (error) {
    ElMessage.error('登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  router.push('/login')
}

const initRemember = () => {
  const remembered = localStorage.getItem('rememberedUser')
  if (remembered) {
    const user = JSON.parse(remembered)
    loginForm.username = user.username
    loginForm.remember = true
  }
}

initRemember()
</script>

<style scoped>
/* 全局容器 - 更换全新科技感背景 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('https://picsum.photos/id/235/1920/1080');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  position: relative;
}

/* 背景暗化遮罩 */
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 15, 30, 0.5);
  z-index: 0;
}

/* 登录框 - 彻底重构样式 */
.login-box {
  width: 420px;
  padding: 50px 45px;
  background: rgba(255, 255, 255, 0.97);
  border-radius: 16px;
  box-shadow: 0 15px 40px rgba(0, 30, 60, 0.2);
  backdrop-filter: blur(15px);
  position: relative;
  z-index: 1;
  border: 1px solid rgba(140, 200, 255, 0.2);
}

/* 标题区域 - 新增装饰元素 */
.login-header {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
}

.header-decoration {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 6px;
  background: linear-gradient(90deg, #1e88e5, #64b5f6);
  border-radius: 3px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #0d47a1;
  margin-bottom: 12px;
  letter-spacing: 0.8px;
}

.login-subtitle {
  font-size: 15px;
  color: #4299e1;
  margin: 0;
  font-weight: 500;
}

/* 表单样式 */
.login-form {
  margin-bottom: 30px;
}

/* 自定义输入框 */
.input-custom {
  border-radius: 12px !important;
  border: 1px solid #e3f2fd !important;
  height: 54px !important;
  font-size: 16px !important;
  background-color: #f8f9fa !important;
  transition: all 0.3s ease;
}

.input-custom:focus-within {
  border-color: #2196f3 !important;
  box-shadow: 0 0 0 4px rgba(33, 150, 243, 0.1) !important;
  background-color: #ffffff !important;
}

.input-icon {
  color: #2196f3 !important;
  font-size: 20px !important;
  margin-left: 4px !important;
}

/* 选项区域 */
.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  font-size: 15px;
}

.checkbox-custom {
  color: #0d47a1 !important;
  font-weight: 500;
}

.link-custom {
  color: #2196f3 !important;
  font-weight: 600;
  transition: color 0.3s ease;
}

.link-custom:hover {
  color: #1976d2 !important;
  text-decoration: underline !important;
}

/* 登录按钮 - 全新渐变样式 */
.btn-login {
  width: 100%;
  height: 54px !important;
  font-size: 18px !important;
  font-weight: 600 !important;
  border-radius: 12px !important;
  background: linear-gradient(90deg, #1976d2, #2196f3) !important;
  border: none !important;
  box-shadow: 0 8px 20px rgba(33, 150, 243, 0.2) !important;
  transition: all 0.3s ease;
}

.btn-login:hover {
  background: linear-gradient(90deg, #1565c0, #1976d2) !important;
  transform: translateY(-3px);
  box-shadow: 0 12px 25px rgba(33, 150, 243, 0.3) !important;
}

.btn-login:active {
  transform: translateY(0);
  box-shadow: 0 6px 15px rgba(33, 150, 243, 0.25) !important;
}

.loading-icon {
  margin-right: 8px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 底部装饰 */
.login-footer {
  margin-top: 20px;
}

.footer-line {
  width: 80px;
  height: 2px;
  background: linear-gradient(90deg, transparent, #2196f3, transparent);
  margin: 0 auto;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    width: 100%;
    padding: 40px 25px;
    border-radius: 12px;
  }

  .login-title {
    font-size: 24px;
  }

  .input-custom,
  .btn-login {
    height: 50px !important;
  }

  .login-form {
    margin-bottom: 25px;
  }

  .login-options {
    margin-bottom: 25px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .login-box {
    background: rgba(15, 23, 42, 0.95);
    border-color: rgba(33, 150, 243, 0.3);
  }

  .login-title {
    color: #90caf9;
  }

  .login-subtitle {
    color: #64b5f6;
  }

  .input-custom {
    background-color: rgba(30, 41, 59, 0.8) !important;
    border-color: rgba(33, 150, 243, 0.2) !important;
    color: #ffffff !important;
  }

  .checkbox-custom {
    color: #90caf9 !important;
  }
}
</style>